
#include "commons.h"
#include "trials.h"

unsigned char text7[]={0x53,0x5F,0x2E,0x5F,0x52,0x5F,0x5F,0x5F,0x4D,0x5F,0x5F,0x5F,0x64,0x5F,0x5F,0x25};//I_O__R____ M____

unsigned char text7_2[]={0x4D,0x31,0x5F,0x5F,0x52,0x5F,0x5F,0x5F,0x4D,0x5F,0x5F,0x5F,0x64,0x5F,0x5F,0x25};//M1
unsigned char text7_3[]={0x4E,0x4D,0x31,0x5F,0x52,0x5F,0x5F,0x5F,0x4D,0x5F,0x5F,0x5F,0x64,0x5F,0x5F,0x25};//NM1
unsigned char text7_4[]={0x4D,0x32,0x5F,0x5F,0x52,0x5F,0x5F,0x5F,0x4D,0x5F,0x5F,0x5F,0x64,0x5F,0x5F,0x25};//M2
unsigned char text7_5[]={0x52,0x61,0x6E,0x5F,0x52,0x5F,0x5F,0x5F,0x4D,0x5F,0x5F,0x5F,0x64,0x5F,0x5F,0x25};

unsigned char text8[]={0x53,0x5F,0x5F,0x5F,0x57,0x5F,0x5F,0x5F,0x43,0x5F,0x5F,0x5F,0x74,0x5F,0x5F,0x5F};//S____LT_________
unsigned char text8_2[]={0x4E,0x5F,0x5F,0x5F,0x57,0x5F,0x5F,0x5F,0x43,0x5F,0x5F,0x5F,0x74,0x5F,0x5F,0x5F};
unsigned char text9[]={0x4C,0x52,0x5F,0x5F,0x4C,0x4D,0x5F,0x5F,0x4C,0x57,0x5F,0x5F,0x4C,0x43,0x5F,0x5F};
unsigned char text10[]={0x52,0x5F,0x5F,0x4D,0x5F,0x5F,0x57,0x5F,0x5F,0x43,0x5F,0x5F,0x74,0x5F,0x5F,0x5F};

unsigned char text11[]={0x53,0x31}; // S1
unsigned char text12[]={0x53,0x32}; // S2
unsigned char text13[]={0x53,0x33}; // S3
unsigned char text14[]={0x53,0x34}; // S4
unsigned char text15[]={0x53,0x35}; // S5

struct flagStruct Flags;


int ValDisVer1;
int ValDisVer2;

unsigned int hit=0,miss=0, falseAlarm=0,correctRejection=0,od=0,in=0,correctRatio=0;       //???mice??????
int currentMiss = 0;
unsigned int  r2=0,m2=0,c2=0,w2=0;






void Dispversion(int firstN, int secondN)
{
           // displaying version of code
		   int ValDisVer1 = firstN;
		   int ValDisVer2 = secondN;
           counts_dispose( ValDisVer1  );
	       LCD_set_xy(2,1);
  		   lcd_data(  ValDisVer1 + 0x30 );
           counts_dispose( ValDisVer2  );
	       LCD_set_xy(4,1);
  		   lcd_data(  ValDisVer2 + 0x30 );


}
void newversion(int firstN, int secondN, int thirdN)
{
           // displaying version of code
		   int ValDisVer1 = firstN;
		   int ValDisVer2 = secondN;
		   int ValDisVer3 = thirdN;

  		   counts_dispose( ValDisVer1  );
	       LCD_set_xy(2,1);
  		   lcd_data(  ValDisVer1 + 0x30 );

  		   counts_dispose( ValDisVer2  );
	       LCD_set_xy(3,1);
  		   lcd_data(  ValDisVer2 + 0x30 );

           counts_dispose( ValDisVer3  );
	       LCD_set_xy(4,1);
  		   lcd_data(  ValDisVer3 + 0x30 );


}



//--------------------????----------------------------------------------------------
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
void test_valve (int port)
{
  	home_clr();
  	while(1)
 	{
  		Valve_ON_w_BNC(port,period*2*1,2);
	}
}

void auto_test_valve(void)
{
	home_clr();
	int itr;
	while(1)
	{
		for (itr=1;itr<=16;itr++)
		{
			Valve_ON_w_BNC(itr,period*2*1,2);
			DelayNmSecNew(1000);
			Valve_OFF_w_BNC(itr,2);
			DelayNmSecNew(1000);
		}

	}
}
void test_BNC(void)
{
	NewDigt_out(1,1);
    NewDigt_out(2,1);
    NewDigt_out(3,1);
    NewDigt_out(4,1);
    NewDigt_out(5,1);
    NewDigt_out(6,1);
}
void OdorSPSignalSender(int Odor,int value)
{
	if (Odor==odor_A){localSendOnce(SpOdor_A,value);}
	else if (Odor==odor_B){localSendOnce(SpOdor_B,value);}
	else if (Odor==odor_C){localSendOnce(SpOdor_C,value);}
	else if (Odor==odor_D){localSendOnce(SpOdor_D,value);}
}
int OneOdorJudger(int Odor,int Rule)
{
	if (Rule==1){return 1;} //  ???
	else if (Rule==2){return 0;} // ???
	else if (Rule==3) // odor B,D,F,H??
	{
		if (Odor==odor_B || Odor==odor_D || Odor==odor_F || Odor==odor_H){return 0;}
		else {return 1;}
	}
	else if (Rule==4) // odor B,D,F,H??
	{
		if (Odor==odor_B || Odor==odor_D || Odor==odor_F || Odor==odor_H){return 1;}
		else {return 0;}
	}
	else if (Rule==5) //??odor A,B, ???????
	{
		if (Odor==odor_B || Odor==odor_A){return 0;}
		else {return 1;}
	}
	else if (Rule==6) {//??odor B????????
		if (Odor==odor_B){return 0;}
                else {return 1;}
        }
        return 0;
}
void OneOdorResponse(int Odor,int OdorBNC,float OdorStart, float OdorEnd,int WaterBNC,float WaterStart,float WaterEnd, float WholeLen,int Rule)
{
	int tempResult;
	if (OneOdorJudger(Odor,Rule)){tempResult=2;}
	else {tempResult=4;}
	int TrialTime;int Timer;
	for(TrialTime=0;TrialTime<WholeLen*1000;TrialTime=TrialTime+100)
	{
		Timer=timerCounterI;
		Valve_OFF_w_BNC(water_bitter,WaterBNC);
		if ((TrialTime>WaterEnd*1000.0+501.0 || TrialTime<WaterStart*1000.0-501.0) && LickHistory)
		{
			Valve_ON_w_BNC(water_bitter,period*2*1,WaterBNC);
		}
		if (TrialTime<OdorStart*1000.0+1.0 && TrialTime>OdorStart*1000.0-1.0 ){Valve_ON_w_BNC(Odor,period*2*1,OdorBNC);OdorSPSignalSender(Odor,1);}
		if (TrialTime<OdorEnd*1000.0+1.0 && TrialTime>OdorEnd*1000.0-1.0 ){Valve_OFF_w_BNC(Odor,OdorBNC);OdorSPSignalSender(Odor,0);}
		if (TrialTime<WaterEnd*1000.0-1.0 && TrialTime>WaterStart*1000.0-1.0 && OneOdorJudger(Odor,Rule)&& LickHistory && tempResult!=1)
		{
			Valve_ON_w_BNC(water_sweet,period*2*1,WaterBNC);
			tempResult=1;
		}
		if (TrialTime<WaterEnd*1000.0-1.0 && TrialTime>WaterStart*1000.0-1.0 && 1-OneOdorJudger(Odor,Rule)&& LickHistory)
		{
			Valve_ON_w_BNC(water_bitter,period*2*1,WaterBNC);
			tempResult=3;
		}
		if (TrialTime<WaterEnd*1000.0+1.0 && TrialTime>WaterEnd*1000.0-1.0 )
		{
			if (OneOdorJudger(Odor,Rule)){Valve_OFF_w_BNC(water_sweet,WaterBNC);}
			else if (1-OneOdorJudger(Odor,Rule)){Valve_OFF_w_BNC(water_bitter,WaterBNC);}
			if (tempResult==1)
			{
				hit++;localSendOnce(SpHit,1);
				counts_dispose(hit);LCD_set_xy(6,1);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);
			}
			else if (tempResult==2)
			{
				miss++;localSendOnce(SpMiss,1);
				counts_dispose(miss);LCD_set_xy(10,1);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);
			}
			else if (tempResult==3)
			{
				falseAlarm++;localSendOnce(SpFalseAlarm,1);
				counts_dispose(falseAlarm);LCD_set_xy(6,2);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);
			}
			else if (tempResult==4)
			{
				correctRejection++;localSendOnce(SpCorrectRejection,1);
				counts_dispose(correctRejection);LCD_set_xy(10,2);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);
			}
		}
		if (LickHistory){localSendOnce(SpLick,1);}
		LickHistory=0;
		DelayNmSecNew(Timer-timerCounterI+100);
	}
}
void OneOdorResponseWithTwoTrigger(int Odor,int OdorBNC,float OdorStart, float OdorEnd,int WaterBNC,float WaterStart,float WaterEnd, float WholeLen,int Trigger1,float Trigger1Start,float Trigger1End,int Trigger2,float Trigger2Start,float Trigger2End,int Rule)
{
	int tempResult;
	if (OneOdorJudger(Odor,Rule)){tempResult=2;}//if(1){tempResult=2;}, go trial
	else {tempResult=4;} //if{tempResult=4;}, no-go trial
	int TrialTime;int Timer;
	for(TrialTime=0;TrialTime<WholeLen*1000;TrialTime=TrialTime+100)
	{
		Timer=timerCounterI;
		Valve_OFF_w_BNC(water_bitter,WaterBNC);
		if ((TrialTime>WaterEnd*1000.0+501.0 || TrialTime<WaterStart*1000.0-501.0) && LickHistory)
		{
			Valve_ON_w_BNC(water_bitter,period*2*1,WaterBNC); //if lick during not-reward period, valve4(water_bitter) open
		}
		if (TrialTime<OdorStart*1000.0+1.0 && TrialTime>OdorStart*1000.0-1.0 ){Valve_ON_w_BNC(Odor,period*2*1,OdorBNC);OdorSPSignalSender(Odor,1);} //valve on, odor start
		if (TrialTime<OdorEnd*1000.0+1.0 && TrialTime>OdorEnd*1000.0-1.0 ){Valve_OFF_w_BNC(Odor,OdorBNC);OdorSPSignalSender(Odor,0);}  //valve off, odor end
		if (TrialTime<WaterEnd*1000.0-1.0 && TrialTime>WaterStart*1000.0-1.0 && OneOdorJudger(Odor,Rule)&& LickOnPin && tempResult!=1)
		{
			Valve_ON_w_BNC(water_sweet,period*2*1,WaterBNC);
			tempResult=1;
		} //in go trial, if lick, tempResult=1, hit trial

		if (TrialTime<WaterEnd*1000.0-1.0 && TrialTime>WaterStart*1000.0-1.0 && 1-OneOdorJudger(Odor,Rule)&& LickHistory)
		{
			Valve_ON_w_BNC(water_bitter,period*2*1,WaterBNC);
			tempResult=3;
		}
		if (TrialTime<WaterEnd*1000.0+1.0 && TrialTime>WaterEnd*1000.0-1.0 )
		{
			if (OneOdorJudger(Odor,Rule)){Valve_OFF_w_BNC(water_sweet,WaterBNC);}
			else if (1-OneOdorJudger(Odor,Rule)){Valve_OFF_w_BNC(water_bitter,WaterBNC);}
			if (tempResult==1)
			{
				hit++;localSendOnce(SpHit,1);
				counts_dispose(hit);LCD_set_xy(6,1);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);//if tempResult==1, hit?1
			}
			else if (tempResult==2)
			{
				miss++;localSendOnce(SpMiss,1);
				counts_dispose(miss);LCD_set_xy(10,1);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);//if tempResult==2, miss?1
			}
			else if (tempResult==3)
			{
				falseAlarm++;localSendOnce(SpFalseAlarm,1);
				counts_dispose(falseAlarm);LCD_set_xy(6,2);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);//if tempResult==3, wrong?1
			}
			else if (tempResult==4)
			{
				correctRejection++;localSendOnce(SpCorrectRejection,1);
				counts_dispose(correctRejection);LCD_set_xy(10,2);
				lcd_data(hunds);lcd_data(tens);lcd_data(ones);//if tempResult==4, correct rejection?1
			}
		}
		if (TrialTime<Trigger1Start*1000.0+1.0 && TrialTime>Trigger1Start*1000.0-1.0 ){BNCController(Trigger1,1);} //open the BNC for Trigger1
		if (TrialTime<Trigger1End*1000.0+1.0 && TrialTime>Trigger1End*1000.0-1.0 ){BNCController(Trigger1,0);}//close the BNC for Trigger1
		if (TrialTime<Trigger2Start*1000.0+1.0 && TrialTime>Trigger2Start*1000.0-1.0 ){BNCController(Trigger2,1);}//open the BNC for Trigger2
		if (TrialTime<Trigger2End*1000.0+1.0 && TrialTime>Trigger2End*1000.0-1.0 ){BNCController(Trigger2,0);}//close the BNC for Trigger2

		if (LickHistory){localSendOnce(SpLick,1);}
		LickHistory=0;
		DelayNmSecNew(Timer-timerCounterI+100);
	}
}

int TwoOdorJudger(int Odor1,int Odor2,int Rule)
{
	if (Rule==1)
	{
		if (Odor1==Odor2){return 0;} //if Rule==1, nonmatch, rewarding
		else {return 1;}
	}
	else if (Rule==2)
	{
		if (Odor1==Odor2){return 1;}//if Rule==2, match, rewarding
		else {return 0;}
	}
	else if (Rule==3)
	{
		if (Odor1==odor_B){return 0;} //if Odor1!=odorB, rewarding, AA/AB rewarding when using odorants AB
		else {return 1;}
	}
	else if (Rule==4)
	{
		if (Odor2==odor_B){return 0;}//if Odor2!=odorB, rewarding, BA/AA rewarding when using odorants AB
        else {
            return 1;
        }
    } else if (Rule == 5) {
        int judge1;
        int judge2;
        if (Odor1 == odor_A || Odor1 == odor_D) {
            judge1 = 1;
        } else if (Odor1 == odor_B || Odor1 == odor_E) {
            judge1 = 0;
        }
        if (Odor2 == odor_A || Odor2 == odor_D) {
            judge2 = 1;
        } else if (Odor2 == odor_B || Odor2 == odor_E) {
            judge2 = 0;
        }
        if (judge1 == judge2) {
            return 0;
        } else {
            return 1;
        }
    }
    return 0;
}

void TwoOdorResponse(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Rule) {
    int tempResult;
    if (TwoOdorJudger(Odor1, Odor2, Rule)) {
        tempResult = 2;
    } else {
        tempResult = 4;
    }
    int TrialTime;
    int Timer;
    for (TrialTime = 0; TrialTime < WholeLen * 1000; TrialTime = TrialTime + 100) {
        Timer = timerCounterI;
        Valve_OFF_w_BNC(water_bitter, WaterBNC);
        if ((TrialTime > WaterEnd * 1000.0 + 501.0 || TrialTime < WaterStart * 1000.0 - 501.0) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
        }
        if (TrialTime < FirstOdorStart * 1000.0 + 1.0 && TrialTime > FirstOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor1, period * 2 * 1, Odor1BNC);
            OdorSPSignalSender(Odor1, 1);
        }
        if (TrialTime < FirstOdorEnd * 1000.0 + 1.0 && TrialTime > FirstOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor1, Odor1BNC);
            OdorSPSignalSender(Odor1, 0);
        }
        if (TrialTime < SecondOdorStart * 1000.0 + 1.0 && TrialTime > SecondOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor2, period * 2 * 1, Odor2BNC);
            OdorSPSignalSender(Odor2, 1);
        }
        if (TrialTime < SecondOdorEnd * 1000.0 + 1.0 && TrialTime > SecondOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor2, Odor2BNC);
            OdorSPSignalSender(Odor2, 0);
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory && tempResult != 1) {
            Valve_ON_w_BNC(water_sweet, period * 2 * 1, WaterBNC);
            tempResult = 1;
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && 1 - TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
            tempResult = 3;
        }
        if (TrialTime < WaterEnd * 1000.0 + 1.0 && TrialTime > WaterEnd * 1000.0 - 1.0) {
            if (TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_sweet, WaterBNC);
            } else if (1 - TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_bitter, WaterBNC);
            }
            if (tempResult == 1) {
                hit++;
                localSendOnce(SpHit, 1);
                counts_dispose(hit);
                LCD_set_xy(6, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 2) {
                miss++;
                localSendOnce(SpMiss, 1);
                counts_dispose(miss);
                LCD_set_xy(10, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 3) {
                falseAlarm++;
                localSendOnce(SpFalseAlarm, 1);
                counts_dispose(falseAlarm);
                LCD_set_xy(6, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 4) {
                correctRejection++;
                localSendOnce(SpCorrectRejection, 1);
                counts_dispose(correctRejection);
                LCD_set_xy(10, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        if (LickHistory) {
            localSendOnce(SpLick, 1);
        }
        LickHistory = 0;
        DelayNmSecNew(Timer - timerCounterI + 100);
    }

}

void TwoOdorResponseWithTrigger(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Rule) {
    int tempResult;
    if (TwoOdorJudger(Odor1, Odor2, Rule)) {
        tempResult = 2;
    } else {
        tempResult = 4;
    }
    int TrialTime;
    int Timer;
    for (TrialTime = 0; TrialTime < WholeLen * 1000; TrialTime = TrialTime + 100) {
        Timer = timerCounterI;
        Valve_OFF_w_BNC(water_bitter, WaterBNC);
        if ((TrialTime > WaterEnd * 1000.0 + 501.0 || TrialTime < WaterStart * 1000.0 - 501.0) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
        }
        if (TrialTime < FirstOdorStart * 1000.0 + 1.0 && TrialTime > FirstOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor1, period * 2 * 1, Odor1BNC);
            OdorSPSignalSender(Odor1, 1);
        }
        if (TrialTime < FirstOdorEnd * 1000.0 + 1.0 && TrialTime > FirstOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor1, Odor1BNC);
            OdorSPSignalSender(Odor1, 0);
        }
        if (TrialTime < SecondOdorStart * 1000.0 + 1.0 && TrialTime > SecondOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor2, period * 2 * 1, Odor2BNC);
            OdorSPSignalSender(Odor2, 1);
        }
        if (TrialTime < SecondOdorEnd * 1000.0 + 1.0 && TrialTime > SecondOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor2, Odor2BNC);
            OdorSPSignalSender(Odor2, 0);
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory && tempResult != 1) {
            Valve_ON_w_BNC(water_sweet, period * 2 * 1, WaterBNC);
            tempResult = 1;
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && 1 - TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
            tempResult = 3;
        }
        if (TrialTime < WaterEnd * 1000.0 + 1.0 && TrialTime > WaterEnd * 1000.0 - 1.0) {
            if (TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_sweet, WaterBNC);
            } else if (1 - TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_bitter, WaterBNC);
            }
            if (tempResult == 1) {
                hit++;
                localSendOnce(SpHit, 1);
                counts_dispose(hit);
                LCD_set_xy(6, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 2) {
                miss++;
                localSendOnce(SpMiss, 1);
                counts_dispose(miss);
                LCD_set_xy(10, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 3) {
                falseAlarm++;
                localSendOnce(SpFalseAlarm, 1);
                counts_dispose(falseAlarm);
                LCD_set_xy(6, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 4) {
                correctRejection++;
                localSendOnce(SpCorrectRejection, 1);
                counts_dispose(correctRejection);
                LCD_set_xy(10, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        if (TrialTime < Trigger1Start * 1000.0 + 1.0 && TrialTime > Trigger1Start * 1000.0 - 1.0) {
            BNCController(Trigger1, 1);
        }
        if (TrialTime < Trigger1End * 1000.0 + 1.0 && TrialTime > Trigger1End * 1000.0 - 1.0) {
            BNCController(Trigger1, 0);
        }
        if (LickHistory) {
            localSendOnce(SpLick, 1);
        }
        LickHistory = 0;
        DelayNmSecNew(Timer - timerCounterI + 100);
    }

}

void TwoOdorResponseWithTwoTrigger(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule) {
    int tempResult;
    if (TwoOdorJudger(Odor1, Odor2, Rule)) {
        tempResult = 2;
    } else {
        tempResult = 4;
    }
    int TrialTime;
    int Timer;
    for (TrialTime = 0; TrialTime < WholeLen * 1000; TrialTime = TrialTime + 100) {
        Timer = timerCounterI;
        Valve_OFF_w_BNC(water_bitter, WaterBNC);
        if ((TrialTime > WaterEnd * 1000.0 + 501.0 || TrialTime < WaterStart * 1000.0 - 501.0) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
        }
        if (TrialTime < FirstOdorStart * 1000.0 + 1.0 && TrialTime > FirstOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor1, period * 2 * 1, Odor1BNC);
            OdorSPSignalSender(Odor1, 1);
        }
        if (TrialTime < FirstOdorEnd * 1000.0 + 1.0 && TrialTime > FirstOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor1, Odor1BNC);
            OdorSPSignalSender(Odor1, 0);
        }
        if (TrialTime < SecondOdorStart * 1000.0 + 1.0 && TrialTime > SecondOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor2, period * 2 * 1, Odor2BNC);
            OdorSPSignalSender(Odor2, 1);
        }
        if (TrialTime < SecondOdorEnd * 1000.0 + 1.0 && TrialTime > SecondOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor2, Odor2BNC);
            OdorSPSignalSender(Odor2, 0);
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory && tempResult != 1) {
            Valve_ON_w_BNC(water_sweet, period * 2 * 1, WaterBNC);
            tempResult = 1;
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && 1 - TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
            tempResult = 3;
        }
        if (TrialTime < WaterEnd * 1000.0 + 1.0 && TrialTime > WaterEnd * 1000.0 - 1.0) {
            if (TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_sweet, WaterBNC);
            } else if (1 - TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_bitter, WaterBNC);
            }
            if (tempResult == 1) {
                hit++;
                localSendOnce(SpHit, 1);
                counts_dispose(hit);
                LCD_set_xy(6, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 2) {
                miss++;
                localSendOnce(SpMiss, 1);
                counts_dispose(miss);
                LCD_set_xy(10, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 3) {
                falseAlarm++;
                localSendOnce(SpFalseAlarm, 1);
                counts_dispose(falseAlarm);
                LCD_set_xy(6, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 4) {
                correctRejection++;
                localSendOnce(SpCorrectRejection, 1);
                counts_dispose(correctRejection);
                LCD_set_xy(10, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        if (TrialTime < Trigger1Start * 1000.0 + 1.0 && TrialTime > Trigger1Start * 1000.0 - 1.0) {
            BNCController(Trigger1, 1);
        }
        if (TrialTime < Trigger1End * 1000.0 + 1.0 && TrialTime > Trigger1End * 1000.0 - 1.0) {
            BNCController(Trigger1, 0);
        }
        if (TrialTime < Trigger2Start * 1000.0 + 1.0 && TrialTime > Trigger2Start * 1000.0 - 1.0) {
            BNCController(Trigger2, 1);
        }
        if (TrialTime < Trigger2End * 1000.0 + 1.0 && TrialTime > Trigger2End * 1000.0 - 1.0) {
            BNCController(Trigger2, 0);
        }
        if (LickHistory) {
            localSendOnce(SpLick, 1);
        }
        LickHistory = 0;
        DelayNmSecNew(Timer - timerCounterI + 100);
    }

}

void TwoOdorResponseWithTwoTriggerWithDistructor(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int OdorDistractor, int OdorDistractorBNC, float OdorDistractorStart, float OdorDistractorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule) {
    int tempResult;
    if (TwoOdorJudger(Odor1, Odor2, Rule)) {
        tempResult = 2;
    } else {
        tempResult = 4;
    }
    int TrialTime;
    int Timer;
    for (TrialTime = 0; TrialTime < WholeLen * 1000; TrialTime = TrialTime + 100) {
        Timer = timerCounterI;
        Valve_OFF_w_BNC(water_bitter, WaterBNC);
        if ((TrialTime > WaterEnd * 1000.0 + 501.0 || TrialTime < WaterStart * 1000.0 - 501.0) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
        }
        if (TrialTime < FirstOdorStart * 1000.0 + 1.0 && TrialTime > FirstOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor1, period * 2 * 1, Odor1BNC);
            OdorSPSignalSender(Odor1, 1);
        }
        if (TrialTime < FirstOdorEnd * 1000.0 + 1.0 && TrialTime > FirstOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor1, Odor1BNC);
            OdorSPSignalSender(Odor1, 0);
        }
        if (TrialTime < SecondOdorStart * 1000.0 + 1.0 && TrialTime > SecondOdorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(Odor2, period * 2 * 1, Odor2BNC);
            OdorSPSignalSender(Odor2, 1);
        }
        if (TrialTime < SecondOdorEnd * 1000.0 + 1.0 && TrialTime > SecondOdorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(Odor2, Odor2BNC);
            OdorSPSignalSender(Odor2, 0);
        }
        if (TrialTime < OdorDistractorStart * 1000.0 + 1.0 && TrialTime > OdorDistractorStart * 1000.0 - 1.0) {
            Valve_ON_w_BNC(OdorDistractor, period * 2 * 1, OdorDistractorBNC);
            OdorSPSignalSender(OdorDistractor, 1);
        }
        if (TrialTime < OdorDistractorEnd * 1000.0 + 1.0 && TrialTime > OdorDistractorEnd * 1000.0 - 1.0) {
            Valve_OFF_w_BNC(OdorDistractor, OdorDistractorBNC);
            OdorSPSignalSender(OdorDistractor, 0);
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory && tempResult != 1) {
            Valve_ON_w_BNC(water_sweet, period * 2 * 1, WaterBNC);
            tempResult = 1;
        }
        if (TrialTime < WaterEnd * 1000.0 - 1.0 && TrialTime > WaterStart * 1000.0 - 1.0 && 1 - TwoOdorJudger(Odor1, Odor2, Rule) && LickHistory) {
            Valve_ON_w_BNC(water_bitter, period * 2 * 1, WaterBNC);
            tempResult = 3;
        }
        if (TrialTime < WaterEnd * 1000.0 + 1.0 && TrialTime > WaterEnd * 1000.0 - 1.0) {
            if (TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_sweet, WaterBNC);
            } else if (1 - TwoOdorJudger(Odor1, Odor2, Rule)) {
                Valve_OFF_w_BNC(water_bitter, WaterBNC);
            }
            if (tempResult == 1) {
                hit++;
                localSendOnce(SpHit, 1);
                counts_dispose(hit);
                LCD_set_xy(6, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 2) {
                miss++;
                localSendOnce(SpMiss, 1);
                counts_dispose(miss);
                LCD_set_xy(10, 1);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 3) {
                falseAlarm++;
                localSendOnce(SpFalseAlarm, 1);
                counts_dispose(falseAlarm);
                LCD_set_xy(6, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            } else if (tempResult == 4) {
                correctRejection++;
                localSendOnce(SpCorrectRejection, 1);
                counts_dispose(correctRejection);
                LCD_set_xy(10, 2);
                lcd_data(hunds);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        if (TrialTime < Trigger1Start * 1000.0 + 1.0 && TrialTime > Trigger1Start * 1000.0 - 1.0) {
            BNCController(Trigger1, 1);
        }
        if (TrialTime < Trigger1End * 1000.0 + 1.0 && TrialTime > Trigger1End * 1000.0 - 1.0) {
            BNCController(Trigger1, 0);
        }
        if (TrialTime < Trigger2Start * 1000.0 + 1.0 && TrialTime > Trigger2Start * 1000.0 - 1.0) {
            BNCController(Trigger2, 1);
        }
        if (TrialTime < Trigger2End * 1000.0 + 1.0 && TrialTime > Trigger2End * 1000.0 - 1.0) {
            BNCController(Trigger2, 0);
        }
        if (LickHistory) {
            localSendOnce(SpLick, 1);
        }
        LickHistory = 0;
        DelayNmSecNew(Timer - timerCounterI + 100);
    }

}


//-------------------????-----------------------------------------------------------
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------

void Go_noGo(int Odor1, int Odor1BNC, int Odor2, int Odor2BNC, float OdorStart, float OdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int Odor[2] = {0};
    int OdorBNC[2] = {0};
    Odor[0] = Odor1;
    Odor[1] = Odor2;
    OdorBNC[0] = Odor1BNC;
    OdorBNC[1] = Odor2BNC;

    int sequence = 2;
    int ind;
    int tempInt;
    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 2;
            }
            ind = rand() % sequence;
            OneOdorResponse(Odor[ind], OdorBNC[ind], OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Rule);
            tempInt = Odor[sequence - 1];
            Odor[sequence - 1] = Odor[ind];
            Odor[ind] = tempInt;
            tempInt = OdorBNC[sequence - 1];
            OdorBNC[sequence - 1] = OdorBNC[ind];
            OdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void Go_noGoWithTwoTrigger(int Odor1, int Odor1BNC, int Odor2, int Odor2BNC, float OdorStart, float OdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int Odor[2] = {0};
    int OdorBNC[2] = {0};
    Odor[0] = Odor1;
    Odor[1] = Odor2;
    OdorBNC[0] = Odor1BNC;
    OdorBNC[1] = Odor2BNC;

    int sequence = 2;
    int ind;
    int tempInt;
    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 2;
            }
            ind = rand() % sequence;
            OneOdorResponseWithTwoTrigger(Odor[ind], OdorBNC[ind], OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
            tempInt = Odor[sequence - 1];
            Odor[sequence - 1] = Odor[ind];
            Odor[ind] = tempInt;
            tempInt = OdorBNC[sequence - 1];
            OdorBNC[sequence - 1] = OdorBNC[ind];
            OdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void AB_BA_Go(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[2] = {0};
    int SecondOdor[2] = {0};
    int FirstOdorBNC[2] = {0};
    int SecondOdorBNC[2] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;

    int Rule = 1;
    int sequence = 2;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 2;
            }
            ind = rand() % sequence;
            TwoOdorResponse(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void AA_BB_Go(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[2] = {0};
    int SecondOdor[2] = {0};
    int FirstOdorBNC[2] = {0};
    int SecondOdorBNC[2] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    SecondOdor[0] = Odor1;
    SecondOdor[1] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    SecondOdorBNC[0] = Odor1BNC;
    SecondOdorBNC[1] = Odor2BNC;

    int Rule = 2;
    int sequence = 2;
    int ind;
    int tempInt;
    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 2;
            }
            ind = rand() % sequence;
            TwoOdorResponse(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void TwoOdorDelayMatchTask(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    FirstOdor[2] = Odor1;
    FirstOdor[3] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 4;
            }
            ind = rand() % sequence;
            TwoOdorResponse(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void TwoOdorDelayMatchTaskWithTrigger(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    FirstOdor[2] = Odor1;
    FirstOdor[3] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 4;
            }
            ind = rand() % sequence;
            TwoOdorResponseWithTrigger(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void TwoOdorDelayMatchTaskWithTwoTrigger(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    FirstOdor[2] = Odor1;
    FirstOdor[3] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 4;
            }
            ind = rand() % sequence;
            TwoOdorResponseWithTwoTrigger(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void TwoOdorDelayMatchTaskWithTwoTriggerWithDistructor(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int OdorDistractor, int OdorDistractorBNC, float OdorDistractorStart, float OdorDistractorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    FirstOdor[2] = Odor1;
    FirstOdor[3] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 4;
            }
            ind = rand() % sequence;
            TwoOdorResponseWithTwoTriggerWithDistructor(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, OdorDistractor, OdorDistractorBNC, OdorDistractorStart, OdorDistractorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void PseudoDNMSTasks(int Odor1, int LowOdor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int LowOdor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int OdorDistractor, int OdorDistractorBNC, float OdorDistractorStart, float OdorDistractorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = LowOdor1;
    FirstOdor[1] = LowOdor2;
    FirstOdor[2] = LowOdor1;
    FirstOdor[3] = LowOdor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;

    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 4;
            }
            ind = rand() % sequence;
            TwoOdorResponseWithTwoTriggerWithDistructor(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, OdorDistractor, OdorDistractorBNC, OdorDistractorStart, OdorDistractorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void AutoTwoRuleBlockTask(int Odor1, int Odor1BNC, float FirstOdorStart, float FirstOdorEnd, int Odor2, int Odor2BNC, float SecondOdorStart, float SecondOdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule1, int Rule2, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));



    int FirstOdor[4] = {0};
    int SecondOdor[4] = {0};
    int FirstOdorBNC[4] = {0};
    int SecondOdorBNC[4] = {0};
    FirstOdor[0] = Odor1;
    FirstOdor[1] = Odor2;
    FirstOdor[2] = Odor1;
    FirstOdor[3] = Odor2;
    SecondOdor[0] = Odor2;
    SecondOdor[1] = Odor1;
    SecondOdor[2] = Odor1;
    SecondOdor[3] = Odor2;
    FirstOdorBNC[0] = Odor1BNC;
    FirstOdorBNC[1] = Odor2BNC;
    FirstOdorBNC[2] = Odor1BNC;
    FirstOdorBNC[3] = Odor2BNC;
    SecondOdorBNC[0] = Odor2BNC;
    SecondOdorBNC[1] = Odor1BNC;
    SecondOdorBNC[2] = Odor1BNC;
    SecondOdorBNC[3] = Odor2BNC;

    int sequence = 4;
    int ind;
    int tempInt;
    int Rule = Rule1;
    int LastSessionCR = 0;

    DelayNmSecNew(2 * 1000);
    while (1) {
        if (Rule == Rule1) {
            int sessionnum;
            int trialnum;
            for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
                localSendOnce(SpStepN, 3);
                hit = 0;
                miss = 0;
                falseAlarm = 0;
                correctRejection = 0;
                home_clr();
                puts_lcd((unsigned char*) &text7[0], sizeof (text7));
                LCD_set_xy(2, 1);
                lcd_data(0x72);
                LCD_set_xy(3, 1);
                lcd_data(0x31);
                line_2();
                puts_lcd((unsigned char*) &text8[0], sizeof (text8));
                counts_dispose(sessionnum);
                LCD_set_xy(2, 2);
                lcd_data(tens);
                lcd_data(ones);
                for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
                    if (sequence == 0) {
                        sequence = 4;
                    }
                    ind = rand() % sequence;
                    TwoOdorResponseWithTwoTrigger(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule1);
                    tempInt = FirstOdor[sequence - 1];
                    FirstOdor[sequence - 1] = FirstOdor[ind];
                    FirstOdor[ind] = tempInt;
                    tempInt = SecondOdor[sequence - 1];
                    SecondOdor[sequence - 1] = SecondOdor[ind];
                    SecondOdor[ind] = tempInt;
                    tempInt = FirstOdorBNC[sequence - 1];
                    FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
                    FirstOdorBNC[ind] = tempInt;
                    tempInt = SecondOdorBNC[sequence - 1];
                    SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
                    SecondOdorBNC[ind] = tempInt;
                    sequence--;

                    // Total Trials
                    int totalTrials = hit + correctRejection + miss + falseAlarm;
                    counts_dispose(totalTrials);
                    LCD_set_xy(14, 2);
                    lcd_data(hunds);
                    lcd_data(tens);
                    lcd_data(ones);

                    // Discrimination rate
                    if (hit + correctRejection > 0) {
                        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                        counts_dispose(correctRatio);
                        LCD_set_xy(14, 1);
                        lcd_data(tens);
                        lcd_data(ones);
                    }
                }
                localSendOnce(SpSess, 0);
                if (correctRatio >= 80 && LastSessionCR >= 80 && Rule == Rule1) {
                    Rule = Rule2;
                }
                LastSessionCR = correctRatio;
                if (Rule != Rule1) {
                    LastSessionCR = 0;
                    break;
                }
            }
            LCD_set_xy(2, 1);
            lcd_data(0x54);
            LCD_set_xy(3, 1);
            lcd_data(0x46);
            TwoOdorDelayMatchTaskWithTwoTrigger(Odor1, Odor1BNC, FirstOdorStart, FirstOdorEnd, Odor2, Odor2BNC, SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule1, rand() % 30, 1);
            NewDigt_out(8, 1);
        } else if (Rule == Rule2) {
            int sessionnum;
            int trialnum;
            for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
                localSendOnce(SpStepN, 2);
                hit = 0;
                miss = 0;
                falseAlarm = 0;
                correctRejection = 0;
                home_clr();
                puts_lcd((unsigned char*) &text7[0], sizeof (text7));
                LCD_set_xy(2, 1);
                lcd_data(0x72);
                LCD_set_xy(3, 1);
                lcd_data(0x32);
                line_2();
                puts_lcd((unsigned char*) &text8[0], sizeof (text8));
                counts_dispose(sessionnum);
                LCD_set_xy(2, 2);
                lcd_data(tens);
                lcd_data(ones);
                for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
                    if (sequence == 0) {
                        sequence = 4;
                    }
                    ind = rand() % sequence;
                    TwoOdorResponseWithTwoTrigger(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart, FirstOdorEnd, SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule2);
                    tempInt = FirstOdor[sequence - 1];
                    FirstOdor[sequence - 1] = FirstOdor[ind];
                    FirstOdor[ind] = tempInt;
                    tempInt = SecondOdor[sequence - 1];
                    SecondOdor[sequence - 1] = SecondOdor[ind];
                    SecondOdor[ind] = tempInt;
                    tempInt = FirstOdorBNC[sequence - 1];
                    FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
                    FirstOdorBNC[ind] = tempInt;
                    tempInt = SecondOdorBNC[sequence - 1];
                    SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
                    SecondOdorBNC[ind] = tempInt;
                    sequence--;

                    // Total Trials
                    int totalTrials = hit + correctRejection + miss + falseAlarm;
                    counts_dispose(totalTrials);
                    LCD_set_xy(14, 2);
                    lcd_data(hunds);
                    lcd_data(tens);
                    lcd_data(ones);

                    // Discrimination rate
                    if (hit + correctRejection > 0) {
                        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                        counts_dispose(correctRatio);
                        LCD_set_xy(14, 1);
                        lcd_data(tens);
                        lcd_data(ones);
                    }
                    if (Rule != 2) {
                        break;
                    }
                }
                localSendOnce(SpSess, 0);
                if (correctRatio >= 80 && LastSessionCR >= 80 && Rule == Rule2) {
                    Rule = Rule1;
                }
                LastSessionCR = correctRatio;
                if (Rule != Rule2) {
                    LastSessionCR = 0;
                    break;
                }
            }
            LCD_set_xy(2, 1);
            lcd_data(0x54);
            LCD_set_xy(3, 1);
            lcd_data(0x46);
            TwoOdorDelayMatchTaskWithTwoTrigger(Odor1, Odor1BNC, FirstOdorStart, FirstOdorEnd, Odor2, Odor2BNC, SecondOdorStart, SecondOdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule2, rand() % 30, 1);
            NewDigt_out(8, 1);
        }

    }

}

void TwoOdorVaryingTasks(int Odor1, int Odor1BNC, int Odor2, int Odor2BNC, float FirstOdorStart1, float FirstOdorEnd1, float SecondOdorStart1, float SecondOdorEnd1, float FirstOdorStart2, float FirstOdorEnd2, float SecondOdorStart2, float SecondOdorEnd2, float FirstOdorStart3, float FirstOdorEnd3, float SecondOdorStart3, float SecondOdorEnd3, int Times1, int Times2, int Times3, int WaterBNC, float WaterStart1, float WaterEnd1, float WaterStart2, float WaterEnd2, float WaterStart3, float WaterEnd3, float WholeLen1, float WholeLen2, float WholeLen3, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Trigger3, float Trigger3Start, float Trigger3End, int Rule1, int Rule2, int Rule3, int TrialNuminSession, int SessionNum) {
    localSendOnce(SpS3S2, 1);
    localSendOnce(SpStepN, 3);

    DelayNmSecNew(2 * 1000);

    int tempInt;
    float tempFloat;
    int itr1;
    int itr2;

    int FirstOdor[41] = {0};
    int SecondOdor[41] = {0};
    int FirstOdorBNC[41] = {0};
    int SecondOdorBNC[41] = {0};
    float FirstOdorStart[41] = {0};
    float FirstOdorEnd[41] = {0};
    float SecondOdorStart[41] = {0};
    float SecondOdorEnd[41] = {0};
    int Rule[41] = {0};
    float WaterStart[41] = {0};
    float WaterEnd[41] = {0};
    float WholeLen[41] = {0};
    int Trigger[41] = {0};
    float TriggerStart[41] = {0};
    float TriggerEnd[41] = {0};


    for (itr2 = 1; itr2 <= 4 * (Times1 + Times2 + Times3); itr2++) {
        if (itr2 % 4 == 1) {
            FirstOdor[itr2] = Odor1;
            SecondOdor[itr2] = Odor2;
            FirstOdorBNC[itr2] = Odor1BNC;
            SecondOdorBNC[itr2] = Odor2BNC;
        }
        if (itr2 % 4 == 2) {
            FirstOdor[itr2] = Odor2;
            SecondOdor[itr2] = Odor1;
            FirstOdorBNC[itr2] = Odor2BNC;
            SecondOdorBNC[itr2] = Odor1BNC;
        }
        if (itr2 % 4 == 3) {
            FirstOdor[itr2] = Odor1;
            SecondOdor[itr2] = Odor1;
            FirstOdorBNC[itr2] = Odor1BNC;
            SecondOdorBNC[itr2] = Odor1BNC;
        }
        if (itr2 % 4 == 0) {
            FirstOdor[itr2] = Odor2;
            SecondOdor[itr2] = Odor2;
            FirstOdorBNC[itr2] = Odor2BNC;
            SecondOdorBNC[itr2] = Odor2BNC;
        }

        if (itr2 <= 4 * Times1) {
            FirstOdorStart[itr2] = FirstOdorStart1;
            FirstOdorEnd[itr2] = FirstOdorEnd1;
            SecondOdorStart[itr2] = SecondOdorStart1;
            SecondOdorEnd[itr2] = SecondOdorEnd1;
            WaterStart[itr2] = WaterStart1;
            WaterEnd[itr2] = WaterEnd1;
            WholeLen[itr2] = WholeLen1;
            Trigger[itr2] = Trigger1;
            TriggerStart[itr2] = Trigger1Start;
            TriggerEnd[itr2] = Trigger1End;
            Rule[itr2] = Rule1;
        } else if (itr2 <= 4 * (Times1 + Times2)) {
            FirstOdorStart[itr2] = FirstOdorStart2;
            FirstOdorEnd[itr2] = FirstOdorEnd2;
            SecondOdorStart[itr2] = SecondOdorStart2;
            SecondOdorEnd[itr2] = SecondOdorEnd2;
            WaterStart[itr2] = WaterStart2;
            WaterEnd[itr2] = WaterEnd2;
            WholeLen[itr2] = WholeLen2;
            Trigger[itr2] = Trigger2;
            TriggerStart[itr2] = Trigger2Start;
            TriggerEnd[itr2] = Trigger2End;
            Rule[itr2] = Rule2;
        } else if (itr2 <= 4 * (Times1 + Times2 + Times3)) {
            FirstOdorStart[itr2] = FirstOdorStart3;
            FirstOdorEnd[itr2] = FirstOdorEnd3;
            SecondOdorStart[itr2] = SecondOdorStart3;
            SecondOdorEnd[itr2] = SecondOdorEnd3;
            WaterStart[itr2] = WaterStart3;
            WaterEnd[itr2] = WaterEnd3;
            WholeLen[itr2] = WholeLen3;
            Trigger[itr2] = Trigger3;
            TriggerStart[itr2] = Trigger3Start;
            TriggerEnd[itr2] = Trigger3End;
            Rule[itr2] = Rule2;
        }

    }
    int sequence = 4 * (Times1 + Times2 + Times3);
    int ind;
    for (itr1 = 1; itr1 <= SessionNum; itr1++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(itr1);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);

        DelayNmSecNew(2 * 1000);

        for (itr2 = 1; itr2 <= TrialNuminSession; itr2++) {

            if (sequence == 0) {
                sequence = 4 * (Times1 + Times2 + Times3);
            }
            ind = rand() % sequence;
            TwoOdorResponseWithTrigger(FirstOdor[ind], FirstOdorBNC[ind], FirstOdorStart[ind], FirstOdorEnd[ind], SecondOdor[ind], SecondOdorBNC[ind], SecondOdorStart[ind], SecondOdorEnd[ind], WaterBNC, WaterStart[ind], WaterEnd[ind], WholeLen[ind], Trigger[ind], TriggerStart[ind], TriggerEnd[ind], Rule[ind]);
            tempInt = FirstOdor[sequence - 1];
            FirstOdor[sequence - 1] = FirstOdor[ind];
            FirstOdor[ind] = tempInt;
            tempInt = SecondOdor[sequence - 1];
            SecondOdor[sequence - 1] = SecondOdor[ind];
            SecondOdor[ind] = tempInt;
            tempInt = FirstOdorBNC[sequence - 1];
            FirstOdorBNC[sequence - 1] = FirstOdorBNC[ind];
            FirstOdorBNC[ind] = tempInt;
            tempInt = SecondOdorBNC[sequence - 1];
            SecondOdorBNC[sequence - 1] = SecondOdorBNC[ind];
            SecondOdorBNC[ind] = tempInt;
            tempInt = Trigger[sequence - 1];
            Trigger[sequence - 1] = Trigger[ind];
            Trigger[ind] = tempInt;
            tempInt = Rule[sequence - 1];
            Rule[sequence - 1] = Rule[ind];
            Rule[ind] = tempInt;
            tempFloat = FirstOdorStart[sequence - 1];
            FirstOdorStart[sequence - 1] = FirstOdorStart[ind];
            FirstOdorStart[ind] = tempFloat;
            tempFloat = FirstOdorEnd[sequence - 1];
            FirstOdorEnd[sequence - 1] = FirstOdorEnd[ind];
            FirstOdorEnd[ind] = tempFloat;
            tempFloat = SecondOdorStart[sequence - 1];
            SecondOdorStart[sequence - 1] = SecondOdorStart[ind];
            SecondOdorStart[ind] = tempFloat;
            tempFloat = SecondOdorEnd[sequence - 1];
            SecondOdorEnd[sequence - 1] = SecondOdorEnd[ind];
            SecondOdorEnd[ind] = tempFloat;
            tempFloat = WaterStart[sequence - 1];
            WaterStart[sequence - 1] = WaterStart[ind];
            WaterStart[ind] = tempFloat;
            tempFloat = WaterEnd[sequence - 1];
            WaterEnd[sequence - 1] = WaterEnd[ind];
            WaterEnd[ind] = tempFloat;
            tempFloat = WholeLen[sequence - 1];
            WholeLen[sequence - 1] = WholeLen[ind];
            WholeLen[ind] = tempFloat;
            tempFloat = TriggerStart[sequence - 1];
            TriggerStart[sequence - 1] = TriggerStart[ind];
            TriggerStart[ind] = tempFloat;
            tempFloat = TriggerEnd[sequence - 1];
            TriggerEnd[sequence - 1] = TriggerEnd[ind];
            TriggerEnd[ind] = tempFloat;
            sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void EightOdorGo_noGo(void) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    Go_noGo(odor_C, 2, odor_A, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);
    Go_noGo(odor_D, 2, odor_B, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);
    Go_noGo(odor_E, 2, odor_A, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);
    Go_noGo(odor_F, 2, odor_B, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);
    Go_noGo(odor_G, 2, odor_A, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);
    Go_noGo(odor_H, 2, odor_B, 3, 0.0, 1.0, 4, 1.0, 2.0, 6.0, 5, 20, 1);

}

void RandomGoNogo(int Odor1, int Odor1BNC, int Odor2, int Odor2BNC, float OdorStart, float OdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Rule, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    // displaying version of code
    ValDisVer1 = 3;
    ValDisVer2 = 5;
    counts_dispose(ValDisVer1);
    LCD_set_xy(2, 1);
    lcd_data(ValDisVer1 + 0x30); // d
    counts_dispose(ValDisVer2);
    LCD_set_xy(4, 1);
    lcd_data(ValDisVer2 + 0x30); // d

    int Odor[2] = {0};
    int OdorBNC[2] = {0};
    Odor[0] = Odor1;
    Odor[1] = Odor2;
    OdorBNC[0] = Odor1BNC;
    OdorBNC[1] = Odor2BNC;

    int sequence = 2;
    int ind;
    DelayNmSecNew(2 * 1000);
    int sessionnum;
    int trialnum;
    for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        counts_dispose(sessionnum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);
        for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
            if (sequence == 0) {
                sequence = 2;
            }
            ind = rand() % 2;
            OneOdorResponse(Odor[ind], OdorBNC[ind], OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Rule);
            //tempInt=Odor[sequence-1];
            //Odor[sequence-1]=Odor[ind];
            //Odor[ind]=tempInt;
            //tempInt=OdorBNC[sequence-1];
            //OdorBNC[sequence-1]=Odor[ind];
            //OdorBNC[ind]=tempInt;
            //sequence--;

            // Total Trials
            int totalTrials = hit + correctRejection + miss + falseAlarm;
            counts_dispose(totalTrials);
            LCD_set_xy(14, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

            // Discrimination rate
            if (hit + correctRejection > 0) {
                correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                counts_dispose(correctRatio);
                LCD_set_xy(14, 1);
                lcd_data(tens);
                lcd_data(ones);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void AutoTwoRuleGoNogoTask(int Odor1, int Odor1BNC, int Odor2, int Odor2BNC, float OdorStart, float OdorEnd, int WaterBNC, float WaterStart, float WaterEnd, float WholeLen, int Trigger1, float Trigger1Start, float Trigger1End, int Trigger2, float Trigger2Start, float Trigger2End, int Rule1, int Rule2, int TrialNuminSession, int SessionNum) {
    home_clr();
    puts_lcd((unsigned char*) &text7[0], sizeof (text7));
    line_2();
    puts_lcd((unsigned char*) &text8[0], sizeof (text8));

    int Odor[2] = {0};
    int OdorBNC[2] = {0};
    Odor[0] = Odor1;
    Odor[1] = Odor2;
    OdorBNC[0] = Odor1BNC;
    OdorBNC[1] = Odor2BNC;

    int Rule = Rule1;
    int LastSessionCR = 0;

    DelayNmSecNew(2 * 1000);
    while (1) {
        if (Rule == Rule1) {
            int sequence = 2;
            int ind;
            int tempInt;
            DelayNmSecNew(2 * 1000);
            int sessionnum;
            int trialnum;
            for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
                hit = 0;
                miss = 0;
                falseAlarm = 0;
                correctRejection = 0;
                home_clr();
                puts_lcd((unsigned char*) &text7[0], sizeof (text7));
                LCD_set_xy(2, 1);
                lcd_data(0x72);
                LCD_set_xy(3, 1);
                lcd_data(0x31);
                line_2();
                puts_lcd((unsigned char*) &text8[0], sizeof (text8));
                counts_dispose(sessionnum);
                LCD_set_xy(2, 2);
                lcd_data(tens);
                lcd_data(ones);
                for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
                    if (sequence == 0) {
                        sequence = 2;
                    }
                    ind = rand() % sequence;
                    OneOdorResponseWithTwoTrigger(Odor[ind], OdorBNC[ind], OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
                    tempInt = Odor[sequence - 1];
                    Odor[sequence - 1] = Odor[ind];
                    Odor[ind] = tempInt;
                    tempInt = OdorBNC[sequence - 1];
                    OdorBNC[sequence - 1] = OdorBNC[ind];
                    OdorBNC[ind] = tempInt;
                    sequence--;

                    // Total Trials
                    int totalTrials = hit + correctRejection + miss + falseAlarm;
                    counts_dispose(totalTrials);
                    LCD_set_xy(14, 2);
                    lcd_data(hunds);
                    lcd_data(tens);
                    lcd_data(ones);

                    // Discrimination rate
                    if (hit + correctRejection > 0) {
                        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                        counts_dispose(correctRatio);
                        LCD_set_xy(14, 1);
                        lcd_data(tens);
                        lcd_data(ones);
                    }
                }
                localSendOnce(SpSess, 0);
                if (correctRatio >= 85 && LastSessionCR >= 85 && Rule == Rule1) {
                    Rule = Rule2;
                }
                LastSessionCR = correctRatio;
                if (Rule != Rule1) {
                    LastSessionCR = 0;
                    break;
                }
            }

            LCD_set_xy(2, 1);
            lcd_data(0x54);
            LCD_set_xy(3, 1);
            lcd_data(0x46);
            Go_noGoWithTwoTrigger(Odor1, Odor1BNC, Odor2, Odor2BNC, OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule1, rand() % 30, 1);
            NewDigt_out(8, 1);
        } else if (Rule == Rule2) {
            int sequence = 2;
            int ind;
            int tempInt;
            DelayNmSecNew(2 * 1000);
            int sessionnum;
            int trialnum;
            for (sessionnum = 1; sessionnum <= SessionNum; sessionnum++) {
                hit = 0;
                miss = 0;
                falseAlarm = 0;
                correctRejection = 0;
                home_clr();
                puts_lcd((unsigned char*) &text7[0], sizeof (text7));
                LCD_set_xy(2, 1);
                lcd_data(0x72);
                LCD_set_xy(3, 1);
                lcd_data(0x32);
                line_2();
                puts_lcd((unsigned char*) &text8[0], sizeof (text8));
                counts_dispose(sessionnum);
                LCD_set_xy(2, 2);
                lcd_data(tens);
                lcd_data(ones);
                for (trialnum = 1; trialnum <= TrialNuminSession; trialnum++) {
                    if (sequence == 0) {
                        sequence = 2;
                    }
                    ind = rand() % sequence;
                    OneOdorResponseWithTwoTrigger(Odor[ind], OdorBNC[ind], OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule);
                    tempInt = Odor[sequence - 1];
                    Odor[sequence - 1] = Odor[ind];
                    Odor[ind] = tempInt;
                    tempInt = OdorBNC[sequence - 1];
                    OdorBNC[sequence - 1] = OdorBNC[ind];
                    OdorBNC[ind] = tempInt;
                    sequence--;

                    // Total Trials
                    int totalTrials = hit + correctRejection + miss + falseAlarm;
                    counts_dispose(totalTrials);
                    LCD_set_xy(14, 2);
                    lcd_data(hunds);
                    lcd_data(tens);
                    lcd_data(ones);

                    // Discrimination rate
                    if (hit + correctRejection > 0) {
                        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
                        counts_dispose(correctRatio);
                        LCD_set_xy(14, 1);
                        lcd_data(tens);
                        lcd_data(ones);
                    }
                }
                localSendOnce(SpSess, 0);
                if (correctRatio >= 85 && LastSessionCR >= 85 && Rule == Rule2) {
                    Rule = Rule1;
                }
                LastSessionCR = correctRatio;
                if (Rule != Rule2) {
                    LastSessionCR = 0;
                    break;
                }
            }

            LCD_set_xy(2, 1);
            lcd_data(0x54);
            LCD_set_xy(3, 1);
            lcd_data(0x46);
            Go_noGoWithTwoTrigger(Odor1, Odor1BNC, Odor2, Odor2BNC, OdorStart, OdorEnd, WaterBNC, WaterStart, WaterEnd, WholeLen, Trigger1, Trigger1Start, Trigger1End, Trigger2, Trigger2Start, Trigger2End, Rule2, rand() % 30, 1);
            NewDigt_out(8, 1);
        }

    }

}

void Go(int OdorIn, float OdorLen, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward) // TrialType:Hit or miss
{

    ///////////-about this function-//////////////////
    // FirstOdorIn = LowP OR LowP ONLY
    // SecondOdorIn = LowP OR LowP ONLY
    // TrialTypeIn =1 is Hit/Miss trial(reward) ; TrialTypeIn = 2 is False alarm/correct rejection trial(punishment)
    ///////////-about this function-//////////////////


    ///////////-first odor-/////////////////
    /* NewDigt_out(3,0.5);

     i=0;
     while (i< afterlightt)
     {
     } */ // as starting cue


    Valve_ON_w_BNC(OdorIn, period * 2 * 1.0, 2);

    if (OdorIn == odor_A) localSendOnce(SpOdor_A, 1); // 1 for open
    else if (OdorIn == odor_B) localSendOnce(SpOdor_B, 1); // 1 for open
    else if (OdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (OdorIn == odor_D) localSendOnce(SpOdor_D, 1);
    timerCounterI = 0;
    while (timerCounterI < OdorLen * 1000) {
        // 1  airpuff peroid
        if (LickOnPin) {
            Flags.pun = 1;
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1.0, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;
    //DelayNmSecNew(FirOdorLen*1000);
    Valve_OFF_w_BNC(OdorIn, 2);

    if (OdorIn == odor_A) localSendOnce(SpOdor_A, 0); // 0 for close
    else if (OdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (OdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (OdorIn == odor_D) localSendOnce(SpOdor_D, 0); // 0 for close


    ////////-delay before reward-///////
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) // the delay after second odor
    {
        // 4  airpuff peroid
        if (LickOnPin) {// Flags.pun = 1; //?????????
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1.0, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;




    ///////////-reward-/////////////////
    Flags.lick = 0;

    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Valve_ON_w_BNC(water_sweet, period * 2 * 1, 2);
            localSendOnce(SpWater_sweet, 1);
            Flags.lick = 1;
        }
    }
    Valve_OFF_w_BNC(water_sweet, 2);
    localSendOnce(SpWater_sweet, 0);

    if (Flags.lick) {
        Flags.lick = 0;
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        counts_dispose(hit);
        LCD_set_xy(6, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    } else {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        counts_dispose(miss);
        LCD_set_xy(10, 1);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);

    }
    // Total Trials
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);

    // Discrimination rate
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }



    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        // 6  airpuff peroid
        if (LickOnPin) {//     Flags.pun = 1;
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

}

void Nogo(int OdorIn, float OdorLen, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward) // TrialType:Hit or miss
{

    ///////////-about this function-//////////////////
    // FirstOdorIn = LowP OR LowP ONLY
    // SecondOdorIn = LowP OR LowP ONLY
    // TrialTypeIn =1 is Hit/Miss trial(reward) ; TrialTypeIn = 2 is False alarm/correct rejection trial(punishment)
    ///////////-about this function-//////////////////


    ///////////-first odor-/////////////////
    /* NewDigt_out(3,0.5);

     i=0;
     while (i< afterlightt)
     {
     } */ // as starting cue


    Valve_ON_w_BNC(OdorIn, period * 2 * 1.0, 2);

    if (OdorIn == odor_A) localSendOnce(SpOdor_A, 1); // 1 for open
    else if (OdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (OdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (OdorIn == odor_D) localSendOnce(SpOdor_D, 1); // 1 for open

    timerCounterI = 0;
    while (timerCounterI < OdorLen * 1000) {
        // 1  airpuff peroid
        if (LickOnPin) {
            Flags.pun = 1;
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;
    //DelayNmSecNew(FirOdorLen*1000);
    Valve_OFF_w_BNC(OdorIn, 2);

    if (OdorIn == odor_A) localSendOnce(SpOdor_A, 0); // 0 for close
    else if (OdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (OdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (OdorIn == odor_D) localSendOnce(SpOdor_D, 0); // 0 for close


    ////////-delay before reward-///////
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) // the delay after second odor
    {
        // 4  airpuff peroid
        if (LickOnPin) {// Flags.pun = 1; //?????????
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;


    ///////////- punishment-/////////////////
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            Flags.lick = 1;
            Flags.pun = 1;
        }

        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }
    }

    Flags.pun = 0;
    Flags.punable = 1;


    if (Flags.lick) {
        timerCounterI = 0;
        while (timerCounterI < prolong * 1000) // longer ITI


            Flags.lick = 0; // Reset flag



        falseAlarm++;
        currentMiss = 0;
        localSendOnce(SpFalseAlarm, 1);
        counts_dispose(falseAlarm);
        LCD_set_xy(6, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);


    } else {
        correctRejection++;
        Flags.lick = 0;
        localSendOnce(SpCorrectRejection, 1);
        counts_dispose(correctRejection);
        LCD_set_xy(10, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);
    }

    // Total Trials
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    counts_dispose(totalTrials);
    LCD_set_xy(14, 2);
    lcd_data(hunds);
    lcd_data(tens);
    lcd_data(ones);

    // Discrimination rate
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
        counts_dispose(correctRatio);
        LCD_set_xy(14, 1);
        lcd_data(tens);
        lcd_data(ones);
    }

    int interval = IntervalIn;

    timerCounterI = 0;
    while (timerCounterI < interval * 1000) {

        // 6  airpuff peroid
        if (LickOnPin) {
            Flags.pun = 1;
            localSendOnce(SpLick, 1); ////////////
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;



    ///////////-inter trial interval-/////////////////
}

void air_Go_Nogo(int thisdelay, int interti, int trialNumperSess, float WaterLen, int missstop, int sessstop, float delay_before_reward) {
    localSendOnce(SpStepN, 0);
    DelayNmSecNew(2 * 1000);

    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;

    while ((currentMiss < missstop) && (sessionNum < sessstop)) //while (1) // no stop
    {
        sessionNum++;
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);

        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));

        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);

        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;

        while ((temptrial < (trialNumperSess)) && (currentMiss < missstop)) {
            trial = 0;
            Flags.ab = 0;
            Flags.ba = 0;

            while (trial < 2 && currentMiss < missstop) {
                int ValRan = rand() % 2; //0-1???
                switch (ValRan) {
                    case 0:
                        if (Flags.ab) {
                            break;
                        } else {
                            Go(odor_C, 1.0, WaterLen, interti, 0, delay_before_reward);
                            Flags.ab = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 1:
                        if (Flags.ba) {
                            break;
                        } else {
                            Nogo(odor_D, 1.0, WaterLen, interti, 0, delay_before_reward);
                            Flags.ba = 1;
                            trial++;
                            temptrial++;
                            break;
                        }



                }

            }

        }

        localSendOnce(SpSess, 0);

    }

    localSendOnce(SpTrain, 0); // send it's the end
}

void auto_AB_BA_Go_AA_BB_Nogo_Ran2(int thisdelay, int interti, int trialNumperSess, float WaterLen, int missstop, int sessstop, float delay_before_reward)//HanZhe
{
    localSendOnce(SpStepN, 0);
    DelayNmSecNew(2 * 1000);

    int temptrial = 0;
    int trial = 0;
    int sessionNum = 0;

    while ((currentMiss < missstop) && (sessionNum < sessstop)) //while (1) // no stop
    {
        sessionNum++;
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);

        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));

        counts_dispose(sessionNum);
        LCD_set_xy(2, 2);
        lcd_data(tens);
        lcd_data(ones);

        temptrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        r2 = 0;
        m2 = 0;

        while ((temptrial < (trialNumperSess)) && (currentMiss < missstop)) {
            trial = 0;
            Flags.aca = 0;
            Flags.acb = 0;
            Flags.bca = 0;
            Flags.bcb = 0;
            Flags.ada = 0;
            Flags.adb = 0;
            Flags.bda = 0;
            Flags.bdb = 0;

            while (trial < 8 && currentMiss < missstop) {
                int ValRan = rand() % 8; //0-7???
                switch (ValRan) {
                    case 0:
                        if (Flags.aca) {
                            break;
                        } else {
                            ODNMS2(odor_A, 2, thisdelay, odor_C, 2, odor_A, WaterLen, interti, 0, delay_before_reward);
                            Flags.aca = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 1:
                        if (Flags.acb) {
                            break;
                        } else {
                            ODNMS2(odor_A, 2, thisdelay, odor_C, 2, odor_B, WaterLen, interti, 0, delay_before_reward);
                            Flags.acb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }


                    case 2:
                        if (Flags.bca) {
                            break;
                        } else {
                            ODNMS2(odor_B, 2, thisdelay, odor_C, 2, odor_A, WaterLen, interti, 0, delay_before_reward);
                            Flags.bca = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 3:
                        if (Flags.bcb) {
                            break;
                        } else {
                            ODNMS2(odor_B, 2, thisdelay, odor_C, 2, odor_B, WaterLen, interti, 0, delay_before_reward);
                            Flags.bcb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 4:
                        if (Flags.ada) {
                            break;
                        } else {
                            ODNMS2(odor_A, 2, thisdelay, odor_D, 2, odor_A, WaterLen, interti, 0, delay_before_reward);
                            Flags.ada = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 5:
                        if (Flags.adb) {
                            break;
                        } else {
                            ODNMS2(odor_A, 2, thisdelay, odor_D, 2, odor_B, WaterLen, interti, 0, delay_before_reward);
                            Flags.adb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 6:
                        if (Flags.bda) {
                            break;
                        } else {
                            ODNMS2(odor_B, 2, thisdelay, odor_D, 2, odor_A, WaterLen, interti, 0, delay_before_reward);
                            Flags.bda = 1;
                            trial++;
                            temptrial++;
                            break;
                        }

                    case 7:
                        if (Flags.bdb) {
                            break;
                        } else {
                            ODNMS2(odor_B, 2, thisdelay, odor_D, 2, odor_B, WaterLen, interti, 0, delay_before_reward);
                            Flags.bdb = 1;
                            trial++;
                            temptrial++;
                            break;
                        }



                }

            }

        }

        localSendOnce(SpSess, 0);

    }

    localSendOnce(SpTrain, 0); // send it's the end
}

void ODNMS2(int FirstOdorIn, int FirOdorLen, int OdorDelayIn, int SecondOdorIn, int SecOdorLen, int ThirdOdorIn, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward) //HanZhe
{

    ///////////////FirstOdor/////////////
    Valve_ON_w_BNC(FirstOdorIn, period * 2 * 1.0, 2);
    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 1);

    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        // 1  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF_w_BNC(FirstOdorIn, 2);

    if (FirstOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (FirstOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (FirstOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (FirstOdorIn == odor_D) localSendOnce(SpOdor_D, 0);

    ///////////interval/////////////////

    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        // 2  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;


    ///////////-Second odor-/////////////////

    Valve_ON_w_BNC(SecondOdorIn, period * 2 * 1.0, 2);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 1);

    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        // 3  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;

    Valve_OFF_w_BNC(SecondOdorIn, 2);
    if (SecondOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (SecondOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (SecondOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (SecondOdorIn == odor_D) localSendOnce(SpOdor_D, 0);
    /////////////-reward1-///////////////
    if (SecondOdorIn == odor_C) {

        Flags.lick = 0;

        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON_w_BNC(water_sweet, period * 2 * 1, 2);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF_w_BNC(water_sweet, 2);
        localSendOnce(SpWater_sweet, 0);

        if (Flags.lick) {
            Flags.lick = 0;
            r2++;
        } else {
            m2++;
        }
        // Discrimination rate2
        if (r2 + c2 > 0) {
            correctRatio = 100 * (r2 + c2) / (r2 + m2 + c2 + w2);
            counts_dispose(correctRatio);
            LCD_set_xy(2, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }

    if (SecondOdorIn == odor_D) {
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
                Flags.pun = 1;
            }

            if (Flags.pun == 1 && Flags.punable == 1) {
                Valve_ON_w_BNC(4, period * 2 * 1, 2);
                DelayNmSecNew(200);
                Valve_OFF_w_BNC(4, 2);
                Flags.punable = 0;
            }

        }


        Flags.pun = 0;
        Flags.punable = 1;


        if (Flags.lick) {
            Flags.lick = 0; // Reset flag
            w2++;
        } else {
            c2++;

        }


        // Discrimination rate2
        if (r2 + c2 > 0) {
            correctRatio = 100 * (r2 + c2) / (r2 + m2 + c2 + w2);
            counts_dispose(correctRatio);
            LCD_set_xy(2, 1);
            lcd_data(tens);
            lcd_data(ones);
        }
    }
    ///////////interval/////////////////

    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        // 2  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;

    ////ThirdOdor/////
    Valve_ON_w_BNC(ThirdOdorIn, period * 2 * 1.0, 2);
    if (ThirdOdorIn == odor_A) localSendOnce(SpOdor_A, 1);
    else if (ThirdOdorIn == odor_B) localSendOnce(SpOdor_B, 1);
    else if (ThirdOdorIn == odor_C) localSendOnce(SpOdor_C, 1);
    else if (ThirdOdorIn == odor_D) localSendOnce(SpOdor_D, 1);

    timerCounterI = 0;
    while (timerCounterI < FirOdorLen * 1000) {
        // 1  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;
    Valve_OFF_w_BNC(ThirdOdorIn, 2);

    if (ThirdOdorIn == odor_A) localSendOnce(SpOdor_A, 0);
    else if (ThirdOdorIn == odor_B) localSendOnce(SpOdor_B, 0);
    else if (ThirdOdorIn == odor_C) localSendOnce(SpOdor_C, 0);
    else if (ThirdOdorIn == odor_D) localSendOnce(SpOdor_D, 0);


    ////////-delay before reward-///////
    {
        timerCounterI = 0;
        while (timerCounterI < delay_before_reward * 1000) // the delay after second odor
        {
            // 4  airpuff peroid
            if (LickOnPin) {
                localSendOnce(SpLick, 1); ////////////
                Flags.pun = 1;
            }
            if (Flags.pun == 1 && Flags.punable == 1) {
                Valve_ON_w_BNC(4, period * 2 * 1, 2);
                DelayNmSecNew(200);
                Valve_OFF_w_BNC(4, 2);
                Flags.punable = 0;
            }

        }

        Flags.pun = 0;
        Flags.punable = 1;

    }
    if (FirstOdorIn != ThirdOdorIn) {
        ///////////-reward2-/////////////////
        Flags.lick = 0;

        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON_w_BNC(water_sweet, period * 2 * 1, 2);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF_w_BNC(water_sweet, 2);
        localSendOnce(SpWater_sweet, 0);

        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;

            hit++;
            localSendOnce(SpHit, 1);
            counts_dispose(hit);
            LCD_set_xy(6, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        } else {
            currentMiss++;
            miss++;

            localSendOnce(SpMiss, 1);
            counts_dispose(miss);
            LCD_set_xy(10, 1);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);

        }
        // Total Trials
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);

        // Discrimination rate
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }

    } else {
        ///////////- punishment-/////////////////
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
                Flags.pun = 1;
            }

            if (Flags.pun == 1 && Flags.punable == 1) {
                Valve_ON_w_BNC(4, period * 2 * 1, 2);
                DelayNmSecNew(200);
                Valve_OFF_w_BNC(4, 2);
                Flags.punable = 0;
            }

        }


        Flags.pun = 0;
        Flags.punable = 1;


        if (Flags.lick) {


            Flags.lick = 0; // Reset flag



            falseAlarm++;

            currentMiss = 0;
            localSendOnce(SpFalseAlarm, 1);
            counts_dispose(falseAlarm);
            LCD_set_xy(6, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);


        } else {
            correctRejection++;

            Flags.lick = 0;
            localSendOnce(SpCorrectRejection, 1);
            counts_dispose(correctRejection);
            LCD_set_xy(10, 2);
            lcd_data(hunds);
            lcd_data(tens);
            lcd_data(ones);
        }

        // Total Trials
        int totalTrials = hit + correctRejection + miss + falseAlarm;
        counts_dispose(totalTrials);
        LCD_set_xy(14, 2);
        lcd_data(hunds);
        lcd_data(tens);
        lcd_data(ones);

        // Discrimination rate
        if (hit + correctRejection > 0) {
            correctRatio = 100 * (hit + correctRejection) / (hit + miss + correctRejection + falseAlarm);
            counts_dispose(correctRatio);
            LCD_set_xy(14, 1);
            lcd_data(tens);
            lcd_data(ones);
        }

    }
    //localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        // 6  airpuff peroid
        if (LickOnPin) {
            localSendOnce(SpLick, 1); ////////////
            Flags.pun = 1;
        }
        if (Flags.pun == 1 && Flags.punable == 1) {
            Valve_ON_w_BNC(4, period * 2 * 1, 2);
            DelayNmSecNew(200);
            Valve_OFF_w_BNC(4, 2);
            Flags.punable = 0;
        }

    }

    Flags.pun = 0;
    Flags.punable = 1;

    //localSendOnce(SpITI, 0);

    ///////////-inter trial interval-/////////////////
}

